/**
 * @author lk
 * @date 2022/6/5 10:58
 */
package com.mmall.thread;

import java.io.FileWriter;
import java.io.IOException;
import java.util.IllegalFormatWidthException;

public class PrintThread extends Thread {
    @Override
    public void run() {
        try {
            extracted();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void extracted() throws IOException {

        for (int i = 0; i < 2; i++) {
            String fileName = i + ".xml";
            FileWriter fileWriter = new FileWriter(fileName);
            fileWriter.write(i + "hello");
            fileWriter.close();
        }
    }

    public static void main(String[] args) {
        Window window = new Window();
        for (int i = 0; i < 50; i++) {
            Thread thread = new Thread(window);
            thread.start();
        }
    }

    static class Window implements Runnable {

        private int count = 30;

        @Override
        public void run() {
            synchronized (this) {
                while (count > 0) {
                    sleep();
                    if (count <= 0) {
                        String message = Thread.currentThread().getName() + "获取到" + count + "号";
                        System.out.println(message);
                    }
                    System.out.println(count);
                    count -= 1;

                }
            }
        }

        private void sleep() {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
